package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.data.LinkData;

import models.LinkModel;

public class UpdateLinkServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        String strIds = (String) request.getParameter("ids");
        String action = (String) request.getParameter("action");
        int status = 0;
        String[] ids = strIds.split(",");
        LinkData linkData = new LinkData();
        
        ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("active", "pending", "inactive"));
        
        for (int i = 0; i < ids.length; i++) {
            if (!ids[i].isEmpty()) {
            	if(statusAction.contains(action.toLowerCase())) {
	                if (action.equalsIgnoreCase("active")) {
	                	status = 2;
	                } else if (action.equalsIgnoreCase("pending")) {
						status = 1;
					} else if (action.equalsIgnoreCase("inactive")) {
						status = 0;
					}
	                
	                LinkModel link = linkData.getLink(new Long(ids[i]));
	                if (link != null) {
	        			link = linkData.setStatus(link, status);
	        		}
            	} else if (action.equalsIgnoreCase("remove")) {
					LinkModel link = linkData.getLink(new Long(ids[i]));
					linkData.delete(link);
				}
            }
        }
    }
}
